<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title">授权</h4>
</div>
<form id="defaultForm" method="post" class="form-horizontal" action="/system/role">
    <div class="modal-body">
        <input id="roleId" style="display: none" th:value="${roleId}">
        <ul id="treeDemo" class="ztree"></ul>

    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="button" class="btn btn-primary" onclick="saveAuthorization()">保存</button>
    </div>
</form>

<script th:line="javascript">

    var setting = {
        check: {
            enable: true,
            chkStyle:"checkbox",
            chkboxType:{"Y":"p","N":"s"}
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        async:{
            enable:true,
            dataType:"text",
            url:"/system/role_tree_data?id=[[${roleId}]]",
            type:"post"
        }
    };

    $(document).ready(function(){
        $.fn.zTree.init($("#treeDemo"), setting);

    });

    function saveAuthorization() {
        var roldId = $("#roleId").val();
        var treeIds = "";
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes = treeObj.getCheckedNodes(true);
        for(x in nodes){
            treeIds = nodes[x].id + "," + treeIds;
        }
        treeIds = treeIds.substr(0,treeIds.length - 1);
        $.ajax({
            url:"/system/role_authorization_save",
            data:{"roleId":roldId,"treeIds":treeIds},
            dataType:"json",
            success:function (result) {
                if(result.type == "alert"){
                    alert(result.message);
                }
                $("#modal").modal("hide");
            },
            error:function(){
               alert("error")
            }
        });
    }
</script>